<template>
  <div class="page-form">
    <Form ref="form" :label-width="labelWidth" :model="formData" :rules="ruleValidate">
      <FormItem label="id" prop="id" hidden>
        <Input v-model="formData.id"/>
      </FormItem>
      <FormItem label="theme_json" prop="theme_json" hidden>
        <Input v-model="formData.theme_json"/>
      </FormItem>
      <FormItem label="path" prop="path" hidden>
        <Input v-model="formData.path"/>
      </FormItem>
      <FormItem label="project_id" prop="project_id" hidden>
        <Input v-model="formData.project_id"/>
      </FormItem>
      <FormItem label="component" prop="component" hidden>
        <Input v-model="formData.component"/>
      </FormItem>
      <FormItem label="layout" prop="layout" hidden>
        <Input v-model="formData.layout"/>
      </FormItem>
      <FormItem label="thumbnail" prop="thumbnail" hidden>
        <Input v-model="formData.thumbnail"/>
      </FormItem>

      <FormItem label="页面名称" prop="name">
        <Input v-model="formData.name" placeholder="页面名称也作为标题显示于浏览器页签栏中" :style="{width: '450px'}"/>
      </FormItem>
      <FormItem label="页面类型" prop="develop_canvas">
<!--        <Select v-model="formData.develop_canvas" :style="{width: '450px'}">
          <Option value="AbsoluteLayoutCanvas">绝对布局</Option>
          <Option value="ReactiveLayoutCanvas">响应式栅格布局</Option>
        </Select>-->
        <RadioGroup v-model="formData.develop_canvas" type="button">
          <Radio label="AbsoluteLayoutCanvas">自由布局式数据大屏</Radio>
          <Radio label="ReactiveLayoutCanvas">响应栅格式数据大屏</Radio>
          <Radio label="FormLayoutCanvas">自定义表单</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="选择模板">
        <div style="margin-bottom: 10px;">
          <RadioGroup v-model="radioGroupValue" type="button" @on-change="loadPageTemplates">
            <Radio :label="'1'">系统模板</Radio>
            <Radio :label="'0'">我的模板</Radio>
          </RadioGroup>
        </div>
        <div class="clearfix"></div>

        <div class="none-alert" v-if="tableData.filter((o)=>{return o.develop_canvas == this.formData.develop_canvas}).length == 0">无相关模板数据</div>

        <Row :gutter="10">
          <i-col span="6" v-for="item in tableDataFilter" :key="item.id">

            <div class="page-template-card">
              <div class="image-wrap">
                <Icon type="ios-image" v-if="!item.thumbnail"/>
                <img v-if="item.thumbnail" :src="$PnUtil.buildImageSrc(item.thumbnail)"
                     v-viewer="{toolbar: false, navbar: false}"/>
              </div>
              <div class="body">
                <p style="line-height: 28px !important; margin-bottom: -5px;">{{item.name}}</p>
                <Tag :color="item.is_sys == '1' ? 'orange':'default'">
                  {{item.is_sys == '1' ? '系统模板':'我的模板'}}
                </Tag>
                <Tag color="primary">{{item.develop_canvas == 'AbsoluteLayoutCanvas' ? '绝对布局':'响应式栅格布局'}}</Tag>
              </div>
              <div class="opera-wrap">
                <Row>
                  <i-col span="24">
                    <div :class="{'active': item.id == currentSelectedTemplateId}"
                         class="opera-btn" @click="selectPageTemplate(item)">
                      {{item.id == currentSelectedTemplateId ? '已选中，再次点击取消':'选择'}}
                    </div>
                  </i-col>
                </Row>
              </div>
            </div>

          </i-col>
        </Row>

        <Divider :style="{marginTop: '10px', marginBottom: '0px'}"/>
        <Page style="text-align: center;" size="small" :total="pageTotal" :current.sync="pageIndex" :page-size="pageSize"
              prev-text="上一页" next-text="下一页" @on-change="loadPageTemplates"/>

      </FormItem>
      <FormItem label="备注">
        <Input v-model="formData.remark" type="textarea" :rows="4"/>
      </FormItem>
    </Form>
  </div>
</template>

<script>
  export default {
    name: 'PageForm',
    props: {
      labelWidth: {
        type: Number,
        default: 80
      }
    },
    data() {
      return {
        formData: {
          id: '',
          project_id: '',
          name: '',
          path: '',
          component: '',
          develop_canvas: 'AbsoluteLayoutCanvas',
          remark: '',
          layout: '',
          theme_json: '',
          thumbnail: ''
        },
        ruleValidate: {
          name: [
            { required: true, message: '页面名称不能为空', trigger: 'blur' }
          ],
          develop_canvas: [
            { required: true, message: '布局类型不能为空', trigger: 'blur' }
          ]
        },

        radioGroupValue: '1',
        currentSelectedTemplateId: '',
        pageIndex: 1,
        pageSize: 8,
        pageTotal: 0,
        tableData: []
      }
    },
    mounted() {

    },
    methods: {

      loadPageTemplates () {
        this.$PnApi.PageTemplateApi.getPageTemplateForCreatePageForm(this.pageIndex, this.pageSize, this.radioGroupValue).then(result => {
          this.tableData = result.data.payload.list;
          this.pageTotal = result.data.payload.total;
        })
      },

      clearPageForm () {
        this.$refs.form.resetFields();
        this.radioGroupValue = '1';
        this.currentSelectedTemplateId = '';
      },

      selectPageTemplate (pageTemplate) {
        if (pageTemplate.id == this.currentSelectedTemplateId) {
          this.currentSelectedTemplateId = '';
          this.formData.layout = '';
          this.formData.theme_json = '';
          this.formData.thumbnail = '';
          if (!this.formData.develop_canvas) {
            this.formData.component = '';
            this.formData.develop_canvas = '';
          }
        }else {
          this.currentSelectedTemplateId = pageTemplate.id;
          this.formData.component = pageTemplate.develop_canvas.replace('Canvas', '');
          this.formData.develop_canvas = pageTemplate.develop_canvas;
          this.formData.thumbnail = pageTemplate.thumbnail;
          this.$PnApi.PageTemplateApi.getPageTemplateLayout(pageTemplate.id).then(result => {
            this.formData.layout = JSON.stringify(this.$PnUtil.reconsitutionLayoutObj(result.data));
          })
          this.$PnApi.PageTemplateApi.getPageTemplateThemeJson(pageTemplate.id).then(result => {
            this.formData.theme_json = JSON.stringify(result.data);
          })
        }
      },

      clearSelectPageTemplate () {
        this.currentSelectedTemplateId = '';
        this.formData.layout = '';
        this.formData.theme_json = '';
        this.formData.thumbnail = '';
        this.tableData = [];
        this.loadPageTemplates()
      }
    },
    computed: {
      tableDataFilter () {
        return this.tableData.filter((o)=>{
          return o.develop_canvas == this.formData.develop_canvas
        })
      }
    },
    watch: {
      'formData.develop_canvas': {
        handler: function (val, oldVal) {
          if (val != oldVal) {
            this.clearSelectPageTemplate()
          }
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .page-template-card {
    width: 100%;
    border: 1px solid #424242;
    position: relative;
    margin-bottom: 10px;
    &:hover {
      box-shadow: 0px 0px 8px #212121;
    }
    .image-wrap {
      width: 100%;
      height: 120px;
      background-color: #757575;
      position: relative;
      i {
        position: absolute;
        left: 50%;
        top: 50%;
        -webkit-transform: translate(-50%,-50%);
        -moz-transform: translate(-50%,-50%);
        transform:translate(-50%,-50%);
        font-size: 70px;
        color: #fff;
      }
      img {
        width: 100%;
        height: 100%;
        cursor: pointer;
      }
    }
    .body {
      background-color: darken(#757575, 30%);
      padding-left: 8px;
      padding-bottom: 3px;
    }
    .opera-wrap {
      width: 100%;
      height: 30px;
      background-color: darken(#424242, 3%);
      .opera-btn {
        width: 100%;
        text-align: center;
        line-height: 30px;
        cursor: pointer;
        &:hover {
          background-color: #42a5f5;
          color: #fff;
        }
        &.active {
          background-color: #8bc34a;
          color: #fff;
        }
      }
    }
  }
</style>
